﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FOModels.Partner;
using Bussiness.Partner;
using System.IO;
using FOSystem.CultureHelper;
using Bussiness.Language;
using FOModels.Language;
namespace FO.Controllers
{
    public class PartnerController : CultureController
    {
        //
        // GET: /Partner/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ShowCase()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Upload(string qqFile)
        {
            // The upload action will be called by the client control
            // for each file that was selected by the user for upload
            var path = Server.MapPath("~/Content/images");
            var file = Path.Combine(path, qqFile);
            using (var output = System.IO.File.Create(file))
            {
                Request.InputStream.CopyTo(output);
            }
            return Json(new { success = true });
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerPartnerGroup()
        {
            return View();
        }
        public JsonResult getPartnerGroupJson()
        {
            PartnerBLL cardbll = new PartnerBLL();
            List<PartnerGroup> list = new List<PartnerGroup>();
            list = cardbll.getPartnerGroupList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdatePartnerGroup(int PartnerGroupID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL bll = new PartnerBLL();
              PartnerGroup saving = new PartnerGroup();
            if (lang.LangID == 1)
            {
                saving = bll.getPartnerGroupByGroupID(PartnerGroupID);
            }
            else
            {
                saving = bll.getPartnerGroupByGroupID_EN(PartnerGroupID);
            }
            Models.PartnerGroup sa = new Models.PartnerGroup();
            sa.PartnerGroupName = saving.PartnerGroupName;
            return View(sa);
        }

        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdatePartnerGroup(Models.PartnerGroup de)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL bll = new PartnerBLL();
            PartnerGroup saving = new PartnerGroup();

            Models.PartnerGroup sa = new Models.PartnerGroup();
            if (lang.LangID == 1)
            {
                saving = bll.getPartnerGroupByGroupID(de.PartnerGroupID);

                if (saving != null)
                {
                    saving.PartnerGroupName = de.PartnerGroupName;
                    bll.UpdatePartnerGroup(saving);
                    return RedirectToAction("ManagerPartnerGroup");
                }
            }
            else
            {
                saving = bll.getPartnerGroupByGroupID_EN(de.PartnerGroupID);
                if (saving != null)
                {
                    saving.PartnerGroupName = de.PartnerGroupName;
                    bll.UpdatePartnerGroup_EN(saving);
                    return RedirectToAction("ManagerPartnerGroup");
                }
            }
            return View();
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertPartnerGroup()
        {
            return View();

        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertPartnerGroup(FO.Models.PartnerGroup de)
        {
            PartnerBLL bll = new PartnerBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            if (lang.LangID == 1)
            {
                if (ModelState.IsValid)
                {
                    PartnerGroup saving = new PartnerGroup();
                    saving.PartnerGroupName = de.PartnerGroupName;
                    bll.InsertPartnerGroup(saving);
                    return RedirectToAction("ManagerPartnerGroup");
                }
            }
            else
            {
                if (ModelState.IsValid)
                {
                    PartnerGroup saving = new PartnerGroup();
                    saving.PartnerGroupName = de.PartnerGroupName;
                    bll.InsertPartnerGroup_EN(saving);
                    return RedirectToAction("ManagerPartnerGroup");
                }
            }
            return View(de);

        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeletePartnerGroup(int PartnerGroupID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL bll = new PartnerBLL();
            PartnerGroup del = new PartnerGroup();
            if (lang.LangID == 1)
            {

                del = bll.getPartnerGroupByGroupID(PartnerGroupID);
                if (del != null)
                {
                    bll.DeletePartnerGroup(del.PartnerGroupID);
                    var result = new { Status = "OK" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var result = new { Status = "Xóa thất bại" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                del = bll.getPartnerGroupByGroupID_EN(PartnerGroupID);
                if (del != null)
                {
                    bll.DeletePartnerGroup_EN(del.PartnerGroupID);
                    var result = new { Status = "OK" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var result = new { Status = "Xóa thất bại" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }

        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerPartner()
        {
            return View();
        }
        public JsonResult getPartnerJson()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL cardbll = new PartnerBLL();
            List<PartnerModels> list = new List<PartnerModels>();
            if (lang.LangID == 1)
            {
                list = cardbll.getPartnerListJson();
            }
            else
            {
                list = cardbll.getPartnerListJson_EN();
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdatePartner(int PartnerID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL bll = new PartnerBLL();
            Models.Partner sa = new Models.Partner();
            List<PartnerGroup> group = new List<PartnerGroup>();
            PartnerModels saving = new PartnerModels();
            if (lang.LangID == 1)
            {
                saving = bll.getPartnerByID(PartnerID);
                group = bll.getPartnerGroupList();
                ViewBag.GroupListID = new SelectList(group, "PartnerGroupID", "PartnerGroupName", saving.PartnerGroupID);
                sa.PartnerName = saving.PartnerName;
                sa.PartnerImg = saving.PartnerImg;
                sa.PartnerUrl = saving.PartnerUrl;
                sa.PartnerGroupID = saving.PartnerGroupID;
                return View(sa);
            }
            else
            {
                saving = bll.getPartnerByID_EN(PartnerID);
                group = bll.getPartnerGroupList_EN();
                ViewBag.GroupListID = new SelectList(group, "PartnerGroupID", "PartnerGroupName", saving.PartnerGroupID);
                sa.PartnerName = saving.PartnerName;
                sa.PartnerImg = saving.PartnerImg;
                sa.PartnerUrl = saving.PartnerUrl;
                sa.PartnerGroupID = saving.PartnerGroupID;
                return View(sa);
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdatePartner(Models.Partner de)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL bll = new PartnerBLL();
            PartnerModels saving = new PartnerModels();
         
            Models.Partner sa = new Models.Partner();
            List<PartnerGroup> group = new List<PartnerGroup>();
            if (lang.LangID == 1)
            {
                group = bll.getPartnerGroupList();
                saving = bll.getPartnerByID(de.PartnerID);
                ViewBag.GroupListID = new SelectList(group, "PartnerGroupID", "PartnerGroupName", saving.PartnerGroupID);
                if (saving != null)
                {
                    saving.PartnerName = de.PartnerName;
                    saving.PartnerImg = de.PartnerImg;
                    saving.PartnerUrl = de.PartnerUrl;
                    saving.PartnerGroupID = de.PartnerGroupID;
                    bll.UpdatePartner(saving);
                    return RedirectToAction("ManagerPartner");
                }
                return View();
            }
            else
            {
                group = bll.getPartnerGroupList_EN();
                saving = bll.getPartnerByID_EN(de.PartnerID);
                ViewBag.GroupListID = new SelectList(group, "PartnerGroupID", "PartnerGroupName", saving.PartnerGroupID);
                if (saving != null)
                {
                    saving.PartnerName = de.PartnerName;
                    saving.PartnerImg = de.PartnerImg;
                    saving.PartnerUrl = de.PartnerUrl;
                    saving.PartnerGroupID = de.PartnerGroupID;
                    bll.UpdatePartner_EN(saving);
                    return RedirectToAction("ManagerPartner");
                }
                return View();
            }
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertPartner()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL bll = new PartnerBLL();
            List<PartnerGroup> group = new List<PartnerGroup>();
            if (lang.LangID == 1)
            {
                group = bll.getPartnerGroupList();
                ViewBag.GroupListID = new SelectList(group, "PartnerGroupID", "PartnerGroupName");
                return View();
            }
            else
            {
                group = bll.getPartnerGroupList_EN();
                ViewBag.GroupListID = new SelectList(group, "PartnerGroupID", "PartnerGroupName");
                return View();
            }

        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertPartner(FO.Models.Partner de)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL bll = new PartnerBLL();
            List<PartnerGroup> group = new List<PartnerGroup>();
            if (lang.LangID == 1)
            {
                group = bll.getPartnerGroupList();
                ViewBag.GroupListID = new SelectList(group, "PartnerGroupID", "PartnerGroupName");
                if (ModelState.IsValid)
                {
                    PartnerModels saving = new PartnerModels();
                    saving.PartnerName = de.PartnerName;
                    saving.PartnerImg = de.PartnerImg;
                    saving.PartnerUrl = de.PartnerUrl;
                    saving.PartnerGroupID = de.PartnerGroupID;
                    bll.InsertPartner(saving);
                    return RedirectToAction("ManagerPartner");
                }
                return View(de);
            }
            else
            {
                group = bll.getPartnerGroupList_EN();
                ViewBag.GroupListID = new SelectList(group, "PartnerGroupID", "PartnerGroupName");
                if (ModelState.IsValid)
                {
                    PartnerModels saving = new PartnerModels();
                    saving.PartnerName = de.PartnerName;
                    saving.PartnerImg = de.PartnerImg;
                    saving.PartnerUrl = de.PartnerUrl;
                    saving.PartnerGroupID = de.PartnerGroupID;
                    bll.InsertPartner_EN(saving);
                    return RedirectToAction("ManagerPartner");
                }
                return View(de);
            }

        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeletePartner(int PartnerID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PartnerBLL bll = new PartnerBLL();
            PartnerModels del = new PartnerModels();
            if (lang.LangID == 1)
            {
                del = bll.getPartnerByID(PartnerID);
                if (del != null)
                {
                    bll.DeletePartner(del.PartnerID);
                    var result = new { Status = "OK" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var result = new { Status = "Xóa thất bại" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                del = bll.getPartnerByID_EN(PartnerID);
                if (del != null)
                {
                    bll.DeletePartner_EN(del.PartnerID);
                    var result = new { Status = "OK" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var result = new { Status = "Xóa thất bại" };
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }

        }
    }
}
